<template>
  <div>
    <portal-header />
    <div style="width: 100%;background: RGBA(151,190,252,.2);">
      <div class="w-content d-flex flex-column flex-jc-sb flex-ai-c h-padding">
        <div class="login-info-layout">
          <h2 class="login-title color-main">{{ systemName }}</h2>
        </div>
        <el-card class="login-form-layout">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            label-position="left"
            auto-complete="on"
          >
            <div class="info-login">
              <img :src="logoImg">
            </div>

            <el-form-item prop="username">
              <el-input
                ref="username"
                v-model="loginForm.username"
                type="text"
                placeholder="请输入用户名"
                auto-complete="on"
              >
                <span slot="prefix">
                  <svg-icon icon-class="user" class="color-main" />
                </span>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="Password"
                name="password"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              >
                <span slot="prefix">
                  <svg-icon icon-class="password" class="color-main" />
                </span>
                <span slot="suffix" @click="showPwd">
                  <svg-icon icon-class="eye" class="color-main" />
                </span>
              </el-input>
            </el-form-item>
            <el-form-item style="margin-bottom: 20px">
              <el-button
                style="width: 100%"
                type="primary"
                :loading="loading"
                @click.native.prevent="handleLogin"
              >
                登录
              </el-button>
            </el-form-item>

            <!--            <el-form-item style="margin-bottom: 40px">-->
            <!--              <el-button style="width: 100%" type="primary" @click.native.prevent="gotoRegister">-->
            <!--                企业注册>>-->
            <!--              </el-button>-->
            <!--            </el-form-item>-->
            <div style="margin-bottom: 10px;text-align: center;font-size: 16px !important;">
              主办单位: {{ copyright }}
            </div>
          </el-form>
        </el-card>
      </div>
    </div>
    <portal-footer />
  </div>
</template>

<script>
import PortalHeader from '../../portal/components/PortalHeader.vue'
import PortalFooter from '../../portal/components/PortalFooter.vue'
import { copyright, title } from '@/settings'

export default {
  name: 'Login',
  components: {
    PortalHeader,
    PortalFooter
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      callback()
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位！'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      logoImg: require('@/assets/portal_images/logo.png'),
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      systemName: title,
      copyright: copyright
    }
  },
  watch: {
    $route: {
      handler(route) {
        const { query } = route
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    gotoRegister() {
      this.$router.push({ path: '/company/register', query: {}})
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
    // afterQRScan() {
    //   if (e.key === 'x-platform-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>
<style lang="scss">
@import '~@/styles/portal/common';
</style>
<style lang="scss" scoped>
.h-padding {
  padding: 100px 0;
}

.login-info-layout {
  width: 500px;
  line-height: 50px;
  height: 100px;
}

.info-login {
  text-align: center;
  margin: 0 auto;
  margin-bottom: 20px;

  img {
  }
}

.login-form-layout {
  width: 400px;
  -webkit-box-shadow: 0 0 15px 20px rgb(0 47 112 / 36%);
  box-shadow: 0 0 15px 20px rgb(0 47 112 / 36%);
  border-radius: 5px;
}

.login-title {
  text-align: center;
  padding: 0;
  font-size: 24px;
}

.login-center-layout {
  background: #409EFF;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}
</style>
